package com.fenghuajueli.idiomppp.ui.fragment.shiyi;


import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.fenghuajueli.idiomppp.R;
import com.fenghuajueli.idiomppp.adapter.IdiomShiYiListAdapter;
import com.fenghuajueli.idiomppp.entity.IdiomShiYiEntity;
import com.fenghuajueli.libbasecoreui.mvp.LifecycleBaseFragment;

import java.util.List;

/**
 * 成语释义界面
 */
public class IdiomShiYiFragment extends LifecycleBaseFragment<IdiomShiYiPresenter> implements IdiomShiYiView {


    ListView idiomShiYiListView;
    ImageView lastPage;
    ImageView nextPage;
    ProgressBar loadingView;

    private IdiomShiYiListAdapter idiomShiYiListAdapter;


    @Override
    public IdiomShiYiPresenter initPresenter() {
        return new IdiomShiYiPresenter(getActivity());
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        idiomShiYiListView = (ListView) findViewById(R.id.idiomShiYiListView);
        lastPage = (ImageView) findViewById(R.id.lastPage);
        nextPage = (ImageView) findViewById(R.id.nextPage);
        loadingView = (ProgressBar) findViewById(R.id.loadingView);
        loadingView.setVisibility(View.VISIBLE);
        presenter.initData();
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_idiom_shi_yi;
    }


    public static IdiomShiYiFragment newInstance() {
        Bundle args = new Bundle();
        IdiomShiYiFragment fragment = new IdiomShiYiFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public void OnClick(View view) {

        int id = view.getId();
        if (id == R.id.nextPage) {
            presenter.loadNextPageData();
        } else if (id == R.id.lastPage) {
            presenter.loadLastPageData();
        }
    }


    @Override
    public void loadSuccess(List<IdiomShiYiEntity> idiomShiYiEntityList) {
        loadingView.setVisibility(View.GONE);
        if (idiomShiYiListAdapter == null) {
            nextPage.setVisibility(View.VISIBLE);
            idiomShiYiListAdapter = new IdiomShiYiListAdapter(getActivity(), idiomShiYiEntityList);
            idiomShiYiListView.setAdapter(idiomShiYiListAdapter);
        } else {
            idiomShiYiListAdapter.refreshData(idiomShiYiEntityList);
        }
    }

    @Override
    public void changeBtnUpView(int viewStatus) {
        lastPage.setVisibility(viewStatus);
    }

    @Override
    public void changeBtnNextView(int viewStatus) {
        nextPage.setVisibility(viewStatus);
    }
}
